<?php

namespace App\Admin\Controllers;

use App\Models\RecordBalance;
use App\Models\Store;
use Encore\Admin\Actions\Action;
use Illuminate\Support\Arr;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class RecordBalanceConctroller extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '余额消费记录';
    protected $type = [1 => '充值', 2 => '支付'];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new RecordBalance());

        $grid->column('id', __('Id'));
        $grid->column('store.name', __('Store id'));
        $grid->column('member.name', __('Member id'));
        $grid->column('price', __('Price'))->display(function ($data) {
            return $data = '￥' . $data;
        });
        $grid->column('type', __('Type'))
            ->using($this->type)->dot([1 => 'info', 2 => 'success']);
        $grid->column('balance', __('Balance'))->display(function ($data) {
            return $data = '￥' . $data;
        });
        $grid->column('cashier.name', __('Cashier id'));
        $grid->column('description', __('Description'));
        $grid->column('created_at', __('Created at'));

        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->like('member.name', __('Member id'));
            $filter->equal('store_id', __('Store id'))
                ->select(Store::all()->pluck('name', 'id'));
            $filter->equal('type', __('Type'))->radio(Arr::add($this->type, '', '全部'));
        });
        $grid->disableCreateButton();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(RecordBalance::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('store_id', __('Store id'));
        $show->field('member_id', __('Member id'));
        $show->field('price', __('Price'));
        $show->field('type', __('Type'));
        $show->field('balance', __('Balance'));
        $show->field('tables', __('Tables'));
        $show->field('order_id', __('Order id'));
        $show->field('cashier_id', __('Cashier id'));
        $show->field('description', __('Description'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('deleted_at', __('Deleted at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new RecordBalance());

        $form->text('store.name', __('Store id'));
        $form->text('member.name', __('Member id'));
        $form->currency('price', __('Price'))->symbol('￥');
        $form->radio('type', __('Type'))->options($this->type)->default(1);
        $form->currency('balance', __('Balance'))->symbol('￥');
        $form->text('cashier.name', __('Cashier id'));
        $form->text('description', __('Description'));

        $form->footer(function (Form\Footer $footer) {
            $footer->disableReset();
            $footer->disableEditingCheck();
            $footer->disableSubmit();
        });
        return $form;
    }
}
